package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.*;
import com.jy.wms.service.WmsPickingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: WmsPickingController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author syh
 * @Date Wed Oct 25 18:25:37 CST 2017
 */
@Controller
@RequestMapping("/wmspicking")
@Api(value = "/wmspicking", description = "关于拣货、发运管理操作")
public class  WmsPickingController extends BaseController<WmsPickingService,WmsPicking> {
    /**
     * 下发拣货任务前的信息验证（1：可以下发；2：不可以下发）
     * @param wmsPickingList
     * @param session
     * @return
     */
    @PostMapping(value = "/inspectPick")
    @ApiOperation(value = "拣货管理 下发拣货任务信息验证",response = JSONObject.class, notes = "POST下发拣货任务信息验证")
    public @ResponseBody JSONObject inspectPick(@RequestBody List<WmsPicking> wmsPickingList , HttpSession session){
        JSONObject json=new JSONObject();
        try{
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            String sshzid=null;
            if(hz.size()>0){
                sshzid=hz.get(0).getHzid();
            }
            String mc=baseService.inspectPick(sshzid,ckid,wmsPickingList);
            json.put("mc",mc);
        }catch (Exception e){
            logger.info("下发拣货任务信息验证失败");
            e.printStackTrace();
        }
        return json;
    }
        /**
         * 拣货
         * @param setAndModifyModel
         * @param session
         * @return
         */
    @PostMapping(value = "/pick")
    @ApiOperation(value = "拣货管理 拣货信息",response = JSONObject.class, notes = "POST拣货信息")
    public @ResponseBody JSONObject  pickWmsicking(@RequestBody SetAndModifyModel setAndModifyModel , HttpSession session) {
        JSONObject json = getJSONObject();
        try{
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            String sshzid=null;
            if(hz.size()>0){
                sshzid=hz.get(0).getHzid();
            }
            baseService.pickWmsicking(sshzid,userinfo,setAndModifyModel.getIds());
        } catch (SucessException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "拣货成功", "", "");
        } catch (RuntimeException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"拣货失败",e.getMessage(),"");
        } catch (ServiceException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"拣货失败",e.getMessage(),"");
        } catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"拣货失败","拣货失败","");
        }
        return json;
    }

    /**
     * 追加拣货
     * @param setAndModifyModel
     * @param session
     * @return
     */
    @PostMapping(value = "/pickAdd")
    @ApiOperation(value = "追加拣货管理 追加拣货信息",response = JSONObject.class, notes = "POST追加拣货信息")
    public @ResponseBody JSONObject  AddPicking(@RequestBody SetAndModifyModel setAndModifyModel , HttpSession session) {
        JSONObject json = getJSONObject();
        try{
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            String sshzid=null;
            if(hz.size()>0){
                sshzid=hz.get(0).getHzid();
            }
            baseService.AddPicking(sshzid,userinfo,setAndModifyModel.getIds());
        } catch (SucessException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "追加拣货成功", "", "");
        } catch (ServiceException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"追加拣货失败",e.getMessage(),"");
        } catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"追加拣货失败","追加拣货失败","");
        }
        return json;
    }
/******************************拣货任务********************************/
    /**
     * 查询发运订单信息(生成拣货任务专用)
     * @param wmsPicking
     * @param session
     * @return
     */
    @PostMapping(value = "/pickingTaskList")
    @ApiOperation(value = "生成拣货任务 查询发运订单信息", response = JSONObject.class, notes = "POST查询发运订单信息")
    public @ResponseBody
    JSONObject pickingTaskList(@RequestBody WmsPicking wmsPicking, HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主信息
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size() > 0) {
                wmsPicking.setCkid(ckid);
                wmsPicking.setSshzid(hz.get(0).getHzid());
            }
            List<WmsPicking> list = baseService.pickingTaskList(wmsPicking);
            Integer size = list.size();
            if (list.size() > 0) {
                //开始的数量
                Integer start = wmsPicking.getRows() * (wmsPicking.getPage() - 1);
                //结束的数量
                Integer end = wmsPicking.getRows() * wmsPicking.getPage();
                if (end > list.size()) {
                    end = list.size();
                }
                list = list.subList(start, end);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(), size);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 生成拣货任务(生成拣货任务专用)
     *
     * @param wmsPickingList
     * @param session
     * @return
     */
    @PostMapping(value = "/createPickingTask")
    @ApiOperation(value = "生成拣货任务 生成拣货任务信息", response = JSONObject.class, notes = "POST生成拣货任务信息")
    public @ResponseBody
    JSONObject createPickingTask(@RequestBody List<WmsPicking> wmsPickingList, HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主信息
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = null;
            String hzmc = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                hzmc = hz.get(0).getShow_hzid();
            }
            baseService.createPickingTask(wmsPickingList, hzid, hzmc, ckid);
        } catch (SucessException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "生成拣货任务成功", "", "");
        } catch (ServiceException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "生成拣货任务失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "生成拣货任务失败", "生成拣货任务失败", "");
        }
        return json;
    }

    /**
     * 生成拣货任务 保存拣货信息(dps回传)
     *
     * @param wmsPickingTaskDps
     * @param session
     * @return
     */
    @PostMapping(value = "/savePicking")
    @ApiOperation(value = "生成拣货任务 保存拣货信息(dps回传)", response = JSONObject.class, notes = "POST保存拣货信息")
    public @ResponseBody
    JSONObject savePicking(@RequestBody WmsPickingTaskDps wmsPickingTaskDps, HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主信息
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
            }
            /*baseService.savePicking();*/
       /* } catch (SucessException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "生成拣货任务成功", "", "");
        } catch (RuntimeException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "生成拣货任务失败", e.getMessage(), "");
        } catch (ServiceException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "生成拣货任务失败", e.getMessage(), "");
        */} catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "生成拣货任务失败", "生成拣货任务失败", "");
        }
        return json;
    }


    /**
     * 打印分拣单查询
     * @param wmsPrintPicking
     * @param session
     * @return
     */
    @PostMapping(value = "/printPicking")
    @ApiOperation(value = "生成拣货任务 打印分拣单查询", response = JSONObject.class, notes = "POST打印分拣单查询")
    public @ResponseBody
    JSONObject printPicking(@RequestBody WmsPrintPicking wmsPrintPicking, HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主信息
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
            }
            wmsPrintPicking.setCkid(ckid);
            wmsPrintPicking.setHzid(hzid);
            List<WmsPrintPicking> wmsPrintPickingList = baseService.printPicking(wmsPrintPicking);
            json = SetStatus.setSearchType(JSONArray.fromObject(wmsPrintPickingList).toString(), wmsPrintPickingList.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 打印合并分拣单查询
     *
     * @param wmsPrintPicking
     * @param session
     * @return
     */
    @PostMapping(value = "/printMergePicking")
    @ApiOperation(value = "生成拣货任务 打印合并分拣单查询", response = JSONObject.class, notes = "POST打印合并分拣单查询")
    public @ResponseBody
    JSONObject printMergePicking(@RequestBody WmsPrintPicking wmsPrintPicking, HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主信息
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
            }
            wmsPrintPicking.setCkid(ckid);
            wmsPrintPicking.setHzid(hzid);
            wmsPrintPicking = baseService.printMergePicking(wmsPrintPicking);
            json = JSONObject.fromObject(wmsPrintPicking);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 查看已完成拣货任务
     *
     * @param
     * @param fyddid
     * @param session
     * @return
     */
    @GetMapping(value = "/seeCompleteTask/{fyddid}")
    @ApiOperation(value = "生成拣货任务 查看已完成拣货任务", response = JSONObject.class, notes = "GET查看已完成拣货任务")
    public @ResponseBody
    JSONObject seeCompleteTask(@PathVariable("fyddid") String fyddid, HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主信息
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            List<WmsPickingTask> wmsPickingTaskList = null;
            if (hz.size() > 0) {
                String hzid = hz.get(0).getHzid();
                wmsPickingTaskList = baseService.seeCompleteTask(hzid, ckid, fyddid);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(wmsPickingTaskList).toString(), wmsPickingTaskList.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查看未完成拣货任务
     *
     * @param
     * @param fyddid
     * @param session
     * @return
     */
    @GetMapping(value = "/notCompleteTask/{fyddid}")
    @ApiOperation(value = "生成拣货任务 查看未完成拣货任务", response = JSONObject.class, notes = "GET查看未完成拣货任务")
    public @ResponseBody
    JSONObject notCompleteTask(@PathVariable("fyddid") String fyddid, HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主信息
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            List<WmsPickingTask> wmsPickingTaskList = null;
            if (hz.size() > 0) {
                String hzid = hz.get(0).getHzid();
                wmsPickingTaskList = baseService.notCompleteTask(hzid, ckid, fyddid);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(wmsPickingTaskList).toString(), wmsPickingTaskList.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    /***********************************发运*****************************************/
        /**
         * 发运
         * @param setAndModifyModel
         * @param session
         * @return
         */
        @PostMapping(value = "/despacth")
        @ApiOperation(value = "发运管理 发运信息",response = JSONObject.class, notes = "POST发运信息")
        public @ResponseBody JSONObject  despacthWmsicking(@RequestBody SetAndModifyModel setAndModifyModel , HttpSession session) {
            JSONObject json = getJSONObject();
            try{
                UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
                List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
                String sshzid=null;
                if(hz.size()>0){
                    sshzid=hz.get(0).getHzid();
                }
                baseService.despacthWmsicking(sshzid,userinfo,setAndModifyModel.getIds());
             } catch (SucessException e){
               json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运成功", "", "");
            } catch (RuntimeException e){
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"发运失败",e.getMessage(),"");
            }catch (ServiceException e){
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"发运失败",e.getMessage(),"");
            }catch (Exception e){
                e.printStackTrace();
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"发运失败","发运失败","");
            }
            return json;
        }
    /**
     * 发运 判断是否存在待拣货数据
     * @param setAndModifyModel
     * @param session
     * @return
     */
    @PostMapping(value = "/dataJudgment")
    @ApiOperation(value = "发运管理 判断是否存在待拣货数据",response = JSONObject.class, notes = "POST发运信息")
    public @ResponseBody
    Integer dataJudgment(@RequestBody SetAndModifyModel setAndModifyModel , HttpSession session) {
        //i=1数据有待拣货数据；i=2没有待拣货数据
        Integer i=2;
        try{
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            String sshzid=null;
            if(hz.size()>0){
                sshzid=hz.get(0).getHzid();
            }
            i=baseService.dataJudgment(sshzid,ckid,setAndModifyModel.getIds());
        } catch (Exception e){
            logger.info("发运管理 判断是否存在待拣货数据"+e.getMessage());
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"发运失败","发运失败","");
        }
        return i;
    }
    /**
     * 追加发运
     * @param setAndModifyModel
     * @param session
     * @return
     */
    @PostMapping(value = "/despacthAdd")
    @ApiOperation(value = "追加发运管理 发运信息",response = JSONObject.class, notes = "POST追加发运信息")
    public @ResponseBody JSONObject  despacthAddWmsicking(@RequestBody SetAndModifyModel setAndModifyModel , HttpSession session) {
        JSONObject json = getJSONObject();
        try{
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            String sshzid=null;
            if(hz.size()>0){
                sshzid=hz.get(0).getHzid();
            }
            baseService.despacthAddWmsicking(sshzid,userinfo,setAndModifyModel.getIds());
        } catch (SucessException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "追加发运成功", "", "");
        } catch (ServiceException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"追加发运失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"追加发运失败","追加发运失败","");
        }
        return json;
    }

}
